#!/bin/bash

set -euo pipefail

run_login_shell() {
    local shell_bin="$1"
    if [ -x "$shell_bin" ]; then
        if "$shell_bin" -ilc 'echo $PATH'; then
            return 0
        fi
        if "$shell_bin" -lc 'echo $PATH'; then
            return 0
        fi
    fi
    return 1
}

# 1. 优先尝试用户默认 SHELL
if [ -n "${SHELL:-}" ] && run_login_shell "$SHELL"; then
    exit 0
fi

# 2. 常见 shell 备选
for candidate in /bin/zsh /usr/bin/zsh /bin/bash /usr/bin/bash; do
    if run_login_shell "$candidate"; then
        exit 0
    fi
done

# 3. 回退到手动 source 各配置文件
if [ -f "$HOME/.zprofile" ]; then
    source "$HOME/.zprofile"
fi
if [ -f "$HOME/.zshrc" ]; then
    source "$HOME/.zshrc"
fi
if [ -f "$HOME/.bash_profile" ]; then
    source "$HOME/.bash_profile"
fi
if [ -f "$HOME/.bashrc" ]; then
    source "$HOME/.bashrc"
fi
if [ -f "$HOME/.profile" ]; then
    source "$HOME/.profile"
fi

echo "$PATH"